#!/bin/bash

# x11vnc 剪贴板调试开关控制脚本
# 用于快速启用或禁用剪贴板调试功能

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="$SCRIPT_DIR/vnc-source-code/x11vnc/src"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 显示帮助信息
show_help() {
    echo "x11vnc 剪贴板调试开关控制脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -b, --basic     启用基础调试（默认已启用）"
    echo "  -d, --detail    启用详细字符分析调试"
    echo "  -a, --all       启用所有调试功能"
    echo "  -o, --off       禁用所有调试功能"
    echo "  -s, --status    显示当前调试状态"
    echo "  -c, --compile   修改后自动编译"
    echo "  -h, --help      显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 --detail     # 启用详细字符分析"
    echo "  $0 --all --compile  # 启用所有调试并重新编译"
    echo "  $0 --off        # 禁用所有调试"
    echo "  $0 --status     # 查看当前状态"
}

# 显示当前状态
show_status() {
    echo -e "${BLUE}=== 当前调试状态 ===${NC}"
    
    # 检查基础调试开关
    if [ -f "$SOURCE_DIR/clipboard_debug.c" ]; then
        local basic_debug=$(grep "int clipboard_debug_enabled = " "$SOURCE_DIR/clipboard_debug.c" | sed 's/.*= \([0-9]\+\);.*/\1/')
        if [ "$basic_debug" = "1" ]; then
            echo -e "基础调试开关: ${GREEN}✅ 已启用${NC} (clipboard_debug_enabled = 1)"
        else
            echo -e "基础调试开关: ${RED}❌ 已禁用${NC} (clipboard_debug_enabled = 0)"
        fi
    else
        echo -e "基础调试开关: ${YELLOW}⚠️  文件不存在${NC}"
    fi
    
    # 检查详细分析开关
    if [ -f "$SOURCE_DIR/clipboard_utf8.c" ]; then
        local detail_debug=$(grep "int debug_clipboard = " "$SOURCE_DIR/clipboard_utf8.c" | sed 's/.*= \([0-9]\+\);.*/\1/')
        if [ "$detail_debug" = "1" ]; then
            echo -e "详细分析开关: ${GREEN}✅ 已启用${NC} (debug_clipboard = 1)"
        else
            echo -e "详细分析开关: ${RED}❌ 已禁用${NC} (debug_clipboard = 0)"
        fi
    else
        echo -e "详细分析开关: ${YELLOW}⚠️  文件不存在${NC}"
    fi
    
    echo ""
    echo -e "${BLUE}=== 调试级别说明 ===${NC}"
    echo -e "基础调试: 显示剪贴板传输、编码决策等基础信息"
    echo -e "详细分析: 显示字节级字符分析、编码类型判断等详细信息"
    echo ""
    echo -e "${BLUE}=== 使用建议 ===${NC}"
    echo -e "开发调试: 启用所有调试功能"
    echo -e "用户调试: 仅启用基础调试"
    echo -e "生产环境: 禁用所有调试功能"
}

# 修改基础调试开关
set_basic_debug() {
    local value=$1
    local file="$SOURCE_DIR/clipboard_debug.c"
    
    if [ ! -f "$file" ]; then
        echo -e "${RED}错误: 找不到文件 $file${NC}"
        return 1
    fi
    
    # 备份原文件
    cp "$file" "$file.backup.$(date +%Y%m%d_%H%M%S)"
    
    # 修改值
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS
        sed -i '' "s/int clipboard_debug_enabled = [0-9];/int clipboard_debug_enabled = $value;/" "$file"
    else
        # Linux
        sed -i "s/int clipboard_debug_enabled = [0-9];/int clipboard_debug_enabled = $value;/" "$file"
    fi
    
    if [ "$value" = "1" ]; then
        echo -e "${GREEN}✅ 基础调试已启用${NC}"
    else
        echo -e "${RED}❌ 基础调试已禁用${NC}"
    fi
}

# 修改详细分析开关
set_detail_debug() {
    local value=$1
    local file="$SOURCE_DIR/clipboard_utf8.c"
    
    if [ ! -f "$file" ]; then
        echo -e "${RED}错误: 找不到文件 $file${NC}"
        return 1
    fi
    
    # 备份原文件
    cp "$file" "$file.backup.$(date +%Y%m%d_%H%M%S)"
    
    # 修改值
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS
        sed -i '' "s/int debug_clipboard = [0-9];/int debug_clipboard = $value;/" "$file"
    else
        # Linux
        sed -i "s/int debug_clipboard = [0-9];/int debug_clipboard = $value;/" "$file"
    fi
    
    if [ "$value" = "1" ]; then
        echo -e "${GREEN}✅ 详细字符分析已启用${NC}"
    else
        echo -e "${RED}❌ 详细字符分析已禁用${NC}"
    fi
}

# 编译项目
compile_project() {
    echo -e "${BLUE}=== 开始编译项目 ===${NC}"
    
    local script_dir="$SCRIPT_DIR"
    if [ -f "$script_dir/build_static_complete.sh" ]; then
        cd "$script_dir"
        chmod +x build_static_complete.sh
        ./build_static_complete.sh
        
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✅ 编译成功完成${NC}"
        else
            echo -e "${RED}❌ 编译失败${NC}"
            return 1
        fi
    else
        echo -e "${RED}错误: 找不到编译脚本 build_static_complete.sh${NC}"
        return 1
    fi
}

# 主函数
main() {
    local enable_basic=0
    local enable_detail=0
    local disable_all=0
    local show_only_status=0
    local auto_compile=0
    
    # 解析命令行参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            -b|--basic)
                enable_basic=1
                shift
                ;;
            -d|--detail)
                enable_detail=1
                shift
                ;;
            -a|--all)
                enable_basic=1
                enable_detail=1
                shift
                ;;
            -o|--off)
                disable_all=1
                shift
                ;;
            -s|--status)
                show_only_status=1
                shift
                ;;
            -c|--compile)
                auto_compile=1
                shift
                ;;
            -h|--help)
                show_help
                exit 0
                ;;
            *)
                echo -e "${RED}未知选项: $1${NC}"
                show_help
                exit 1
                ;;
        esac
    done
    
    # 检查源码目录
    if [ ! -d "$SOURCE_DIR" ]; then
        echo -e "${RED}错误: 找不到源码目录 $SOURCE_DIR${NC}"
        echo "请确保在正确的项目根目录下运行此脚本"
        exit 1
    fi
    
    # 显示状态
    if [ "$show_only_status" = "1" ] || ([ "$enable_basic" = "0" ] && [ "$enable_detail" = "0" ] && [ "$disable_all" = "0" ] && [ "$auto_compile" = "0" ]); then
        show_status
        exit 0
    fi
    
    echo -e "${BLUE}=== x11vnc 剪贴板调试开关控制 ===${NC}"
    
    # 处理禁用所有调试
    if [ "$disable_all" = "1" ]; then
        echo -e "${YELLOW}正在禁用所有调试功能...${NC}"
        set_basic_debug 0
        set_detail_debug 0
    else
        # 处理启用调试
        if [ "$enable_basic" = "1" ]; then
            echo -e "${YELLOW}正在启用基础调试...${NC}"
            set_basic_debug 1
        fi
        
        if [ "$enable_detail" = "1" ]; then
            echo -e "${YELLOW}正在启用详细字符分析...${NC}"
            set_detail_debug 1
        fi
    fi
    
    echo ""
    show_status
    
    # 自动编译
    if [ "$auto_compile" = "1" ]; then
        echo ""
        compile_project
    else
        echo ""
        echo -e "${BLUE}=== 下一步操作 ===${NC}"
        echo "如果需要重新编译，请运行："
        echo "  $0 --compile"
        echo "或者："
        echo "  cd $SCRIPT_DIR && ./build_static_complete.sh"
    fi
}

# 运行主函数
main "$@"
